package boogie.game;

import boogie.game.sprite.Sprite;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;

/**
 * Basic building block of any game This class represents the state of a game at
 * any one time
 *
 * @author Boogie
 */
public abstract class GameState {

    private Game game;
    public final String name;
    private int fpsRate = 30;
    public final List<Sprite> sprites;

    public GameState(String name) {
        
        this.name = name;
        sprites = new ArrayList<>();
        this.game.addState(name, this);
    }

    /**
     * Called when state is added to a Game
     */
    public abstract void onInit();

    /**
     * Called every frame of the game
     */
    public void onUpdate() {
        for (Sprite s : sprites) {
            s.onUpdate();
            for (Sprite s2 : sprites) {
                if (!s.equals(s2)) {
                    if (s.getHitbox().intersects(s2.getHitbox())) {
                        s.onCollide(s2);
                    }
                }
            }
        }
    }
    /**
     * Called every frame to draw the background of the current game state
     */
    public abstract void drawBackgorund(Graphics2D g);

    /**
     * Called every frame of the game
     */
    public void onDraw(Graphics2D g) {
        this.drawBackgorund(g);
        for (Sprite s : sprites) {
            s.onDraw(g);
        }
    }

    /**
     * Called when Game is paused or when state is switched
     */
    
    
    public void onPause() {
    }

    public Game getGame() {
        return game;
    }

    
    
    /**
     * Called when Game is resumed or when the current game state is set to this
     * one
     */
    public void onResume() {
    }

    /**
     * Called when Game is stopped
     */
    public void onStop() {
    }

    /**
     * Sets the current states FPS rate
     *
     * @param fpsRate - rate of updates/frames per seconds (min 1 and max 1000
     */
    public void setFpsRate(int fpsRate) {
        if (fpsRate > 1000) {
            this.fpsRate = 1000;
            return;
        }
        if (fpsRate <= 0) {
            this.fpsRate = 1;
            return;
        }
        this.fpsRate = fpsRate;
    }

    public int getFpsRate() {
        return fpsRate;
    }
    void setGame(Game g){
        this.game = g;
    }
}
